package lac.serviceuint.tool;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextBean implements ApplicationContextAware{
	private static  ApplicationContext applicationContext;
	
	@SuppressWarnings("static-access")
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}
	
	
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}


	public static Object getBean(String name){
		return applicationContext.getBean(name);
	}
	
	public static <T> T getBean(Class<T> type){
		return applicationContext.getBean(type);
	}
	
	public static <T> List<T> getBeans(Class<T> type){
		List<T> list = new ArrayList<>();
		list.addAll(applicationContext.getBeansOfType(type).values());
		list.remove(null);
		return list;
	}
	
	public static void setBean(Class<?> type){
		AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
		autowireCapableBeanFactory.createBean(type);
	}
}
